var Guid = function () {
    var guid = '';
    for (var i = 1; i <= 32; i++) {
        var n = Math.floor(Math.random() * 16.0).toString(16);
        guid += n;
        if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
            guid += '-';
    }
    return guid;
};
var ajax = function (options, id) {
    var ajaxOptions = $.extend({}, {
        url: '',
        data: {},
        loadingElement: '',
        dataType: 'JSON',
        type: 'GET',
        success: function () { },
        error: null,
    }, options);
    if (ajaxOptions.loadingElement) {
        ajaxOptions.loadingElement.loadingEX();
    };
    return $.ajax({
        url: ajaxOptions.url,
        data: ajaxOptions.data,
        type: ajaxOptions.type,
        dataType: ajaxOptions.options,
        success: function (result) {
            if (ajaxOptions.loadingElement) {
                ajaxOptions.loadingElement.loadingEX();
            };
            ajaxOptions.callBackSuccess(result, ajaxOptions, id);
        },
        error: function (a, b, c) {
            if (ajaxOptions.error) {
                ajaxOptions.callBackError(a, b, c, ajaxOptions, id);
            } else {
                console.log(a, b, c);
            };
        },
    });
};
var request = function () {
    this.list = [];
    this.item = {};
};
request.prototype.POST = function (options) {
    var id = Guid();
    options.type = 'POST';
    options.callBackSuccess = this.callBackSuccess;
    options.callBackError = this.callBackError;
    this.item[id] = ajax(options, id);
    this.list.push(id);
    return id;
};
request.prototype.GET = function (options) {
    var id = Guid();
    options.type = 'GET';
    options.callBackSuccess = this.callBackSuccess;
    options.callBackError = this.callBackError;
    this.item[id] = {};
    this.item[id].options = options;
    this.item[id].ajax = ajax(options, id);
    this.list.push(id);
    return id;
};
request.prototype.callBackSuccess = function (result, options, id) {
    this.removeId(id);
    if (ajaxOptions.loadingElement) {
        ajaxOptions.loadingElement.loadingEX();
    };
    options.success(result);
};
request.prototype.callBackError = function (a, b, c, options, id) {
    this.removeId(id);
    if (ajaxOptions.loadingElement) {
        ajaxOptions.loadingElement.loadingEX();
    };
    options.error(a, b, c);
};

request.prototype.cancel = function (id) {
    if (this.item.hasOwnProperty(id)) {
        var element = this.item[id];
        element.abort();
        this.removeId();
    };
};
request.prototype.cancelAll = function (id) {
    for (var i = 0; i < this.list.length; i++) {
        var id = this.list[i];
        this.cancel(id);
    };
};
request.prototype.removeId = function (id) {
    if (this.item.hasOwnProperty(id)) {
        if (this.item[id].options.loadingElement) {
            this.item[id].options.loadingElement.loadingEX();
        };
        delete this.item[id];
        var index = $.inArray(id, this.list);
        if (index != -1) {
            this.list.splice(index, 1);
        };
    } else {
        console.error('没有找到ID对应的AJAX');
    };
};
export { request }